import org.json.JSONArray;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class PackageMetrics_Main {
    public static void main(String[] args) {
        // 检查是否提供了正确的命令行参数
        if (args.length < 1) {
            System.out.println("请提供JSON文件的地址作为命令行参数");
            return;
        }

        JSONObject jsonObject = (new JsonFileProcess()).getJsonObject(args[0]);

        String name = jsonObject.getString("name");
        JSONArray jsonArray = jsonObject.getJSONArray("variables");

        List<Object> variablesOb = jsonArray.toList();
        List<String> variables = (List<String>)(List)variablesOb;

        // 根据系统路径格式选取相应分离方法
        List<Packages> packages;
        if(variables.get(0).indexOf("\\")!=-1){
            packages = (new SplitPackages()).splitPackages(variables);
        }else{
            packages = (new ExtractPackages()).extractPackages(variables);
        }
        System.out.println("packageSize" + packages.size());

        // 获取cells数组，进行指标计算
        JSONArray jsonArrayCells = jsonObject.getJSONArray("cells");
        Metric metrics[] = new Metric[packages.size()];  // 保存不加权结果
        MetricValues metricValues[] = new MetricValues[packages.size()];  //保存加权结果
        AllMetric allMetric = new AllMetric();  //保存总结果

        // 进行初始化
        int priorClassNum = -1;
        int iMetric = 0;
        for(Packages pack : packages){
            metrics[iMetric] = new Metric();
            metrics[iMetric].packageName = pack.packageName;
            metrics[iMetric].classNumSum = pack.width - priorClassNum;

            metricValues[iMetric] = new MetricValues();
            metricValues[iMetric].packageName = pack.packageName;
            metricValues[iMetric].classNumSum = pack.width - priorClassNum;
            priorClassNum = pack.width;
            iMetric ++;
        }

        String isValue = "unValue";  // 判断是否加权计算
        String isWeight = "unWeight";  //判断结果是加权平均还是算数平均
        if(args.length > 1){
            isValue = args[1];
        }
        if(args.length > 2){
            isWeight = args[2];
        }

        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd'_'HH-mm-ss");
        Date date = new Date(System.currentTimeMillis());
        String outFilePath = "out-" + (new ExtractFileName()).extractFileName(args[0]) + "_" + formatter.format(date) + "_";  // 输出路径及文件名
        if(args.length > 3)
            outFilePath = "out-" + args[3] + "-" + formatter.format(date) ;


        List<String> headers = Arrays.asList("PackagePath&Name", "IIPU", "IIPE", "IPCI", "IIPUD", "IIPED", "PF", "IPSC");
        List<List<String>> outData = new ArrayList<>();

        if(isValue.equals("unValue")){
            if(isWeight.equals("unWeight")){
                outFilePath = outFilePath + "NN" + ".csv";
            }else {
                outFilePath = outFilePath + "NW" + ".csv";
            }

            (new UnValueMetric()).unValueMetrc(jsonArrayCells, packages, metrics, allMetric);
            (new OutputUnValueToCSV()).outputUnValueToCSV(metrics, packages, outData, allMetric, isWeight, variables.size());
            (new WriteDataToCSV()).writeDataToCSV(headers, outData, outFilePath);

            System.out.println("Data written to " + outFilePath + " successfully.");
        }
        else
        {
            if(isWeight.equals("unWeight")){
                outFilePath = outFilePath + "WN" + ".csv";
            }else {
                outFilePath = outFilePath + "WW" + ".csv";
            }

            (new ValueMetric()).valueMetric(jsonArrayCells, packages, metricValues, allMetric);
            (new OutputValueToCSV()).outputValueToCSV(metricValues, packages, outData, allMetric, isWeight, variables.size());
            (new WriteDataToCSV()).writeDataToCSV(headers, outData, outFilePath);

            System.out.println("Data written to " + outFilePath + " successfully.");
        }

    }
}